﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using lutianqi.Model;
using System.ComponentModel;
using Coding4Fun.Phone.Controls;
using Coding4Fun.Phone.Controls.Data;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Shell;

namespace lutianqi
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            if(!DesignerProperties.IsInDesignTool)
            {
                DataContext = App.ViewModel;
                this.Loaded += new RoutedEventHandler(MainPage_Loaded);
                if (App.ViewModel.WeatherInfo.cityid == null)
                {
                    if (ValueHelper.GetValue("CityCode") != null)
                    {
                        App.ViewModel.GetWeatherInfoByCityID(ValueHelper.GetValue("CityCode").ToString());
                    }
                    else
                    {
                        App.ViewModel.GetWeatherInfoByCityID("");
                    }
                }
            }
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            //设置Panorama的默认选项
            if (App.ViewModel.selectIndex.HasValue)
            {
                this.panorama.DefaultItem = this.panorama.Items[App.ViewModel.selectIndex.Value];
            }
        }

        private void LongListSelectionChanged(object sender, RoutedEventArgs e)
        {
            panorama.DefaultItem = panorama.Items[0];
        }

        private void MenuItem_DelClick(object sender, RoutedEventArgs e)
        {
            ((MainViewModel)DataContext).DelFavCommand.Execute((sender as MenuItem).Tag.ToString());
        }

        private void MenuItem_ChangeColorClick(object sender, RoutedEventArgs e)
        {
            ((MainViewModel)DataContext).SetFavoriteColorCommand.Execute((sender as MenuItem).Tag.ToString());
        }

        private void MenuItem_GetWeatherInfoClick(object sender, RoutedEventArgs e)
        {
            ((MainViewModel)DataContext).GetSpecificWeatherInfoCommand.Execute((sender as MenuItem).Tag);
            panorama.DefaultItem = panorama.Items[0];
        }

        private void panorama_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((MainViewModel)DataContext).SetCurrentPanoramaItemNameCommand.Execute(this.panorama.SelectedIndex);
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, CancelEventArgs e)
        {
            MessageBoxResult messageBoxResult = MessageBox.Show("确定要退出吗？", "提示", MessageBoxButton.OKCancel);
            if (messageBoxResult == MessageBoxResult.Cancel)
            {
                e.Cancel = true;
                return;
            }
            base.NavigationService.RemoveBackEntry();
        }

        private void aboutItem_Click(object sender, EventArgs e)
        {
            var p = new AboutPrompt();
            p.VersionNumber = PhoneHelper.GetAppAttribute("Version");
            p.Show("卤面网", null , "wpyouhui@gmail.com", @"http://www.codewp7.com/");
        }

        private void smsItem_Click(object sender, EventArgs e)
        {
            if (App.ViewModel != null)
            {
                WeatherInfoConverted weatherInfo = App.ViewModel.WeatherInfo as WeatherInfoConverted;
                if (null != weatherInfo.cityid)
                {
                    SmsComposeTask smsComposeTask = new SmsComposeTask();
                    string content = weatherInfo.city + "," + weatherInfo.date_y + "," + weatherInfo.week + "," 
                        +(weatherInfo.temp11 == "" ? "" : "白天:" + weatherInfo.temp11) + "夜晚:" + weatherInfo.temp12+"。(来自@卤天气)";
                    smsComposeTask.Body = content;
                    smsComposeTask.Show();
                }
            }
        }
    }
}